package com.commentlib.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.commentlib.app.BaseApplication;

/**
 * 通知类
 */
public class NotificationUtils {
    private final String TAG = this.getClass().getSimpleName();
    private static NotificationUtils utils = null;
    private String NotificationId = "default";

    public static NotificationUtils getInstance(){
        if (utils == null){
            synchronized (NotificationUtils.class){
                if (utils == null){
                    utils = new NotificationUtils();
                }
            }
        }
        return utils;
    }

    /**
     * 构建一个消息
     *
     * @param context 上下文
     * @param icon    图标id
     * @param title   标题
     * @param content 内容
     */
    private NotificationCompat.Builder builderNotification(Context context, int icon, String title, String content) {
        String channel = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = NotificationId;
        }
        return new NotificationCompat.Builder(context, channel)
                .setSmallIcon(icon)
                .setContentTitle(title)
                .setWhen(System.currentTimeMillis())
                .setContentText(content)
                //不能删除
                .setAutoCancel(false)
                //正在交互（如播放音乐）
                .setOngoing(true);
    }

    /**
     * 显示消息通知
     *
     * @param context 上下文
     * @param icon    图标
     * @param title   标题
     * @param content 内容
     */
    public void showAliyunNotification(Context context, int icon, String title, String content, PendingIntent pendingIntent) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            afterO(manager);
        }else {
            getHandSetInfo();
        }
        NotificationCompat.Builder builder = builderNotification(context, icon, title, content)
                .setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentIntent(pendingIntent);
        manager.notify(1011, builder.build());
    }

    /**
     * 取消通知
     *
     * @param context 上下文
     */
    public static void cancelNotification(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(1011);
    }

    /**
     * 适配 Android O 通知渠道
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void afterO(NotificationManager manager) {
        //IMPORTANCE_LOW：默认关闭声音与震动、IMPORTANCE_DEFAULT：开启声音与震动
        NotificationChannel channel = new NotificationChannel(NotificationId, "消息", NotificationManager.IMPORTANCE_HIGH);
        //是否在桌面icon右上角展示小圆点
        channel.enableLights(true);
        //是否显示桌面图标时显示此渠道的通知
        channel.setShowBadge(true);
        //在Android O 不震动
//            channel.enableVibration(true);
        manager.createNotificationChannel(channel);
    }

    private void sanxing(){
        String launcherClassName = getLauncherClassName();
        if (launcherClassName == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", 1);
        intent.putExtra("badge_count_package_name", BaseApplication.activity.getPackageName());
        intent.putExtra("badge_count_class_name", launcherClassName);
        BaseApplication.activity.sendBroadcast(intent);
    }

    private void huawei(){
        String launcherClassName = getLauncherClassName();
        if (launcherClassName != null) {
            Bundle extra = new Bundle();
            extra.putString("package", BaseApplication.activity.getPackageName());
            extra.putString("class", launcherClassName);
            extra.putInt("badgenumber", 1);
            BaseApplication.activity.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
        }
    }

    private void MIUI(){
        Intent intent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
        intent.putExtra("android.intent.action.APPLICATION_MESSAGE_UPDATE","your app packageName/.LAUNCHER ActivityName");
        intent.putExtra("EXTRA_UPDATE_APPLICATION_MESSAGE_TEXT", 1);
        BaseApplication.activity.sendBroadcast(intent);
    }

    private void soney(){
        Intent intent = new Intent();
        String launcherclassname = getLauncherClassName();
        if (launcherclassname != null) {
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
            intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherclassname);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", 1);
            intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", BaseApplication.activity.getPackageName());
            BaseApplication.activity.sendBroadcast(intent);
        }
    }

    private void vivo(){
        String launcherclassname = getLauncherClassName();
        if (launcherclassname != null) {
            Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
            intent.putExtra("packageName", BaseApplication.activity.getPackageName());
            intent.putExtra("className", launcherclassname);
            intent.putExtra("notificationNum", 1);
            BaseApplication.activity.sendBroadcast(intent);
        }
    }

    private void oppo(){
        try {
            Bundle extras = new Bundle();
            extras.putInt("app_badge_count", 1);
            BaseApplication.activity.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
                    "setAppBadgeCount", String.valueOf(1), extras);
        } catch (Throwable th) {}
    }

    public static String getLauncherClassName() {
        if(BaseApplication.activity == null){
            return null;
        }
        PackageManager pm = BaseApplication.activity.getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        try {
            for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
                if (resolveInfo.activityInfo.applicationInfo.packageName.equalsIgnoreCase(BaseApplication.activity.getPackageName())) {
                    return resolveInfo.activityInfo.name;
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    /***获取手机信息**/
    public String getHandSetInfo() {
        String handSetInfo = "手机型号:" + Build.MODEL
                + "\n系统版本:" + Build.VERSION.RELEASE
                + "\n产品型号:" + Build.PRODUCT
                + "\n版本显示:" + Build.DISPLAY
                + "\n系统定制商:" + Build.BRAND + "\n设备参数:" + Build.DEVICE
                + "\n开发代号:" + Build.VERSION.CODENAME + "\nSDK版本号:"
                + Build.VERSION.SDK_INT + "\nCPU类型:" + Build.CPU_ABI
                + "\n硬件类型:" + Build.HARDWARE + "\n主机:"
                + Build.HOST + "\n生产ID:" + Build.ID
                + "\nROM制造商:" + Build.MANUFACTURER // 这行返回的是rom定制商的名称
                ;
        LogUtils.log(handSetInfo);
        return handSetInfo;
    }
}
